package com.aaa.phone.service.authenticationservice.impl;

import com.aaa.entity.AppIdcardApprove;
import com.aaa.entity.AppStcardApprove;
import com.aaa.entity.AppUserinfo;
import com.aaa.entity.BackSchool;
import com.aaa.phone.dao.authenticationdao.IAuthenticationDao;
import com.aaa.phone.service.authenticationservice.AuthenticationService;
import com.aaa.phone.systemconst.ISystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 实名认证和学生认证service实现类
 * @Author: 陈阳
 * @CreateDate: 2018/12/24$ 8:55$
 */
@Service
@Transactional
public class AuthenticationServiceImpl implements AuthenticationService,ISystemConst {

    @Autowired
    private IAuthenticationDao dao;

    /**
     * 实名认证
     */
    @Override
    public int updateAutonymName(AppIdcardApprove appIdcardApprove) {
        //查询实名认证表中是否存在这条记录 防止网络卡顿重复插入记录
        AppIdcardApprove autoExist = dao.getAutoExist(appIdcardApprove.getTelephone());
        if(autoExist==null){
            int autonymAuthentication = dao.toAutonymAuthentication(appIdcardApprove);
            int autonymName = dao.updateAutonymName(appIdcardApprove);
            if (autonymAuthentication > ZERO && autonymName > ZERO) {
                return SUCCESS_CODE;
            }
            return ERROR_CODE;
        }
        return ERROR_CODE;
    }

    /**
     * 学生认证
     */
    @Override
    public int updateSchoolId(AppStcardApprove appStcardApprove) {
        AppStcardApprove stuExist = dao.getStuExist(appStcardApprove.getTelephone());
        if(stuExist==null){
            int toStudentAuthentication = dao.toStudentAuthentication(appStcardApprove);
            int updateSchoolId = dao.updateSchoolId(appStcardApprove);
            if(toStudentAuthentication>ZERO && updateSchoolId>ZERO){
                return SUCCESS_CODE;
            }
            return ERROR_CODE;
        }
        return ERROR_CODE;
    }

    /***
     * 查询实名认证状态
     */
    @Override
    public int queryAA(String telephone) {
        AppUserinfo appUserinfo = dao.queryAA(telephone);
        String none = "无";
        if(appUserinfo.getIdState() == ONE){
            //已实名认证成功100
            return SUCCESS_CODE;
        }else if(none.equals(appUserinfo.getName())){
            //还未进行实名认证-1
            return ERROR_CODE;
        }
        //实名认证已提交且还在审核200.
        return DEFEAT_CODE;
    }

    /**
     * 查询学生认证状态
     */
    @Override
    public int querySA(String telephone) {
        AppUserinfo appUserinfo = dao.queryAA(telephone);
        AppStcardApprove appStcardApprove = dao.querySA(telephone);
        if (appUserinfo.getStudentSate() == ONE) {
            //已学生认证成功100
            return SUCCESS_CODE;
        } else if (appStcardApprove == null) {
            //还未进行学生认证-1
            return ERROR_CODE;
        }
        //学生认证已提交且还在审核200
        return DEFEAT_CODE;
    }

    /**
     * 获取所有的学校
     */
    @Override
    public List<BackSchool> toGetSchool() {
        return dao.toGetSchool();
    }

    /**
     * 获取已经实名认证成功的记录
     */
    @Override
    public AppIdcardApprove toGetAutonymMsg(String telephone) {
        return dao.toGetAutonymMsg(telephone);
    }

    /**
     * 获取已经学生认证成功的记录
     */
    @Override
    public AppStcardApprove toGetStudentMsg(String telephone) {
        return dao.toGetStudentMsg(telephone);
    }

}
